//
//  FKProfileVC.m
//  NeteaseEdu
//
//  Created by Frank.Chan on 16/9/26.
//  Copyright © 2016年 zmosa. All rights reserved.
//

#import "FKProfileVC.h"
#import "UIBarButtonItem+FK.h"
#import "FKCourseCell.h"
#import "FKCourse.h"
#import "UIView+FK.h"
#import "UINavigationBar+FK.h"
#import "FKProfileNavBar.h"
#import <MJExtension.h>
#import <Masonry/Masonry.h>
static CGFloat const headerH = 194;

@interface FKProfileVC () <UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, weak) UITableView *tableView;
@property (nonatomic, strong) NSArray *courses;
@property (nonatomic, weak) UIImageView *headerView;
@property (nonatomic, strong) FKProfileNavBar *profileNavBar;
@end

@implementation FKProfileVC

- (NSArray *)courses
{
    if (!_courses) {
        _courses = [NSArray array];
    }
    return _courses;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = fk_globalGreenColor;
    
    [self setupSubviews];
    [self loadData];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
//    [self.navigationController.navigationBar fk_transparent];
    self.navigationController.navigationBar.hidden = true;
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    
//    [self.navigationController.navigationBar fk_unTransparent];
    self.navigationController.navigationBar.hidden = false;
}

- (void)loadData
{
    NSString *path = [[NSBundle mainBundle] pathForResource:@"profile.json" ofType:nil];
    NSString *str = [NSString stringWithContentsOfFile:path encoding:(NSUTF8StringEncoding) error:nil];
    NSDictionary *dict = [str mj_JSONObject];
    NSArray *resultList = dict[@"results"][@"resultList"];
    NSMutableArray *mutableList = [NSMutableArray array];
    for (NSUInteger index = 0; index < resultList.count; index++) {
        NSDictionary *temp = resultList[index];
        [mutableList addObject:temp[@"yktAggregationVo"]];
    }
    self.courses = [FKCourse mj_objectArrayWithKeyValuesArray:mutableList];
    [self.tableView reloadData];
}

- (void)setupSubviews
{
//    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"btn_green_117x40_"] forBarMetrics:UIBarMetricsDefault];
//    self.navigationItem.leftBarButtonItem = [UIBarButtonItem itemWithTarget:self action:@selector(enterSetting) imageName:@"icon-settings_20x44_" highImageName:@"icon-settings-selected_20x44_"];
//    self.navigationItem.rightBarButtonItem = [UIBarButtonItem itemWithTarget:self action:@selector(enterMessage) imageName:@"message-icon_20x44_" highImageName:@"message-icon-selected_20x44_"];
//    self.navigationItem.titleView = [[UIImageView alloc] init];
//    
//    
//    [self.navigationController.navigationBar fk_transparent];
//    [self.navigationController.navigationBar fk_changeNavigationBarColor:fk_globalGreenColor Alpha:1];
    
    // 创建自定义的navbar
    FKProfileNavBar *profileNavBar = [[FKProfileNavBar alloc] initWithFrame:CGRectMake(0, 0, fk_Width, 64)];
    [self.view addSubview:profileNavBar];
    self.profileNavBar = profileNavBar;
    profileNavBar.backgroundColor = [UIColor redColor];
    
    // 创建tableview的头部
    UIImageView *headerView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, fk_Width, headerH)];
    self.headerView = headerView;
    self.headerView.backgroundColor = fk_globalGreenColor;
    //    headerView.image = [UIImage imageNamed:@"btn_green_117x40_"];
    
    // 创建tableView
    UITableView *tableView = [[UITableView alloc] init];
    [self.view addSubview:tableView];
    [tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(profileNavBar.mas_bottom);
        make.left.equalTo(self.view.mas_left);
        make.right.equalTo(self.view.mas_right);
        make.bottom.equalTo(self.view.mas_bottom).offset(-fk_TabBarHeight);
    }];
    self.tableView = tableView;
    tableView.dataSource = self;
    tableView.delegate = self;
    self.tableView.tableHeaderView = headerView;
}

- (void)enterSetting
{
    
}

- (void)enterMessage
{
    
}

#pragma mark - Table view data source
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.courses.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    FKCourseCell *courseCell = [FKCourseCell courseCellWithTableView:tableView];
    courseCell.course = self.courses[indexPath.row];
    return courseCell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 93;
}

#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
//    // 改变navbar的颜色
//    CGFloat offsetY = scrollView.contentOffset.y;
//    if (offsetY > -fk_NavAndStatusBarHeight) {
//        CGFloat alpha = MIN(1, (offsetY + fk_NavAndStatusBarHeight) / headerH);
//        FKLog(@"--(%f)--", alpha);
//        [self.navigationController.navigationBar fk_changeNavigationBarColor:fk_navGrayColor Alpha:alpha];
//    }
//    
//    // 改变item的图片
//    UIButton *leftItem = (UIButton *)self.navigationItem.leftBarButtonItem.customView;
//    UIButton *rightItem = (UIButton *)self.navigationItem.rightBarButtonItem.customView;
//    if (offsetY > 0) {
//        [leftItem setImage:[UIImage imageNamed:@"setting_gray_20x44_"] forState:UIControlStateNormal];
//        [rightItem setImage:[UIImage imageNamed:@"message-icon-gray_20x44_"] forState:UIControlStateNormal];
//    } else {
//        [leftItem setImage:[UIImage imageNamed:@"icon-settings_20x44_"] forState:UIControlStateNormal];
//        [rightItem setImage:[UIImage imageNamed:@"message-icon_20x44_"] forState:UIControlStateNormal];
//    }
//    
//    // 改变状态栏的颜色
//    if (offsetY > headerH - fk_NavAndStatusBarHeight) {
//        [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleDefault;
//    } else {
//        [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
//    }
}

/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
